//
//  Setting.m
//  ViewPoint
//
//  Created by ldmiao on 09-3-6.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "Setting.h"


@implementation Setting

- (id) init {
	if( (self = [super init]) != nil ){
        //[self initSettings];
	}
	return self;
}

- (void) refresh {
    if(settings!=nil){
        [settings release];
        settings = nil;
    }
	[self initSettings];
}

- (id) valueForKey:(NSString*)key {
	return [settings valueForKey:key];
}

/*
- (id) valueForKey:(NSString*)key inType:(NSString*)type {
	NSString* value = (NSString*)[settings valueForKey:key];
	if([type isEqualToString:@"BOOL"]){
		return [value boolValue];
	}else if([type isEqualToString:@"Integer"]){
		return [value integerValue];
	}
	return value;
}
 */

- (void) setValue:(NSString*)value forKey:(NSString*)key{
	[settings setValue:value forKey:key];
}

- (void) removeObjectForKey:(NSString*)key {
	[settings removeObjectForKey:key];
}

- (void)initSettings {
    settings = [[NSMutableDictionary alloc] initWithContentsOfFile:[Setting dataFilePath]];
    if(settings==nil){
        settings = [[NSMutableDictionary alloc] initWithCapacity:10];
    }
}

- (void) persist {
	[settings writeToFile:[Setting dataFilePath] atomically: YES];
    [settings release];
    settings = nil;
}

- (void) close {
    if(settings!=nil){
        [settings release];
        settings = nil;
    }
}

+ (NSString *)dataFilePath {
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *documentsDirectory = [paths objectAtIndex:0];
	return [documentsDirectory stringByAppendingPathComponent:kFilename];
}

@end
